package main

import (
	"github.com/joho/godotenv"
	"log"
	"mock-build/common"
	"net"
	"net/rpc"
	"os"
)

func main() {
	mock := new(common.Mock)
	arith := new(common.Arith)

	// 注册RPC服务
	err := rpc.Register(mock)
	err = rpc.Register(arith)
	if err != nil {
		log.Println("register rpc error:", err)
	}

	// 在port端口监听
	// 从配置文件中读取服务器ip和port
	err = godotenv.Load(".env")
	if err != nil {
		log.Fatal("Error loading .env file")
	}
	serverPort := os.Getenv("SERVER_PORT")
	if serverPort == "" {
		log.Fatal("Error: wrong port in .env file")
	}
	l, e := net.Listen("tcp", ":"+serverPort)
	if e != nil {
		log.Fatal("listen error:", e)
	}
	log.Println("Listening on", serverPort)

	// 服务连接
	for {
		conn, err := l.Accept()
		if err != nil {
			log.Println("connection error:", err)
			continue
		}
		go rpc.ServeConn(conn)
	}
}
